﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using VirtualPress.Portal.Biz;
using VirtualPress.Infrastructure.Entities.Enum;
using VirtualPress.Infrastructure.Entities.Entity;

namespace VirtualPress.Portal.WebLogic
{
    /// <summary>
    /// 用于构建controller使用的viewbag
    /// </summary>
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class WorkSpaceAttribute : FilterAttribute,IActionFilter
    {
        private UserRole Role { get; set; }
        public WorkSpaceAttribute(UserRole role)
        {
            this.Role = role;
        }
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                return;
            }
            filterContext.Controller.ViewBag.User = new User().Find(filterContext.HttpContext.User.Identity.Name);
            Periodical periodical = null;
            string journal = string.Empty;
            switch (Role)
            {
                case UserRole.Member:
                    filterContext.Controller.ViewBag.ControllerName = "Member";
                    int paymentItemCount = PaymentBiz.GetAllBillForPage(c => c.Own == filterContext.HttpContext.User.Identity.Name && c.Status == (int)BillStatus.DaiZhiFu);
                    filterContext.Controller.ViewBag.PayMentItemCount = paymentItemCount;
                    break;
                case UserRole.MagazineAdmin:
                    journal = filterContext.HttpContext.Request.QueryString["journal"];
                    filterContext.Controller.ViewBag.ControllerName = "Magazine";
                    if (!String.IsNullOrEmpty(journal) && journal != "-1")
                    {
                        periodical = new Periodical().Find(journal);
                    }
                    else
                    {
                        periodical = new Periodical().Find(c => c.Admin == filterContext.HttpContext.User.Identity.Name);
                    }
                    if (periodical == null)
                    {
                        filterContext.HttpContext.Response.Redirect("/workspace/home/index");
                    }
                    filterContext.Controller.ViewBag.Journal = periodical;

                    filterContext.Controller.ViewBag.NewArticleCount = new Article().Count(
                        c => c.Peri_Id == periodical.Id && !c.IsDelete && c.Status == 0);

                    filterContext.Controller.ViewBag.WaitArticleCount = new Article().Count(
                        c => c.Peri_Id == periodical.Id && !c.IsDelete && new List<int>() { 2, 4, 6, 8, 10 }.Contains(c.Status));

                    break;
                case UserRole.JounalsAdmin:
                    journal = filterContext.HttpContext.Request.QueryString["journal"];
                    filterContext.Controller.ViewBag.ControllerName = "Console";

                    if (!String.IsNullOrEmpty(journal) && journal != "-1")
                    {
                        periodical = new Periodical().Find(journal);
                    }
                    else
                    {
                        periodical = new Periodical().Find(c => c.Admin == filterContext.HttpContext.User.Identity.Name);
                    }
                    if (periodical == null)
                    {
                        filterContext.HttpContext.Response.Redirect("/workspace/home/index");
                    }
                    filterContext.Controller.ViewBag.Journal = periodical;

                    filterContext.Controller.ViewBag.NewArticleCount = new Article().Count(
                        c => c.Peri_Id == periodical.Id && !c.IsDelete && c.Status == 0);

                    filterContext.Controller.ViewBag.WaitArticleCount = new Article().Count(
                        c => c.Peri_Id == periodical.Id && !c.IsDelete && new List<int>() { 2, 4, 6, 8, 10 }.Contains(c.Status));

                    break;
                case UserRole.ConferenceAdmin:
                    string meetingId = filterContext.HttpContext.Request.QueryString["meeting"];
                    filterContext.Controller.ViewBag.ControllerName = "Meeting";
                    Meeting meeting = null;
                    if (!String.IsNullOrEmpty(meetingId) && meetingId != "-1")
                    {
                        meeting = new Meeting().Find(meetingId);
                    }
                    else
                    {
                        meeting = new Meeting().Find(c => c.Admin == filterContext.HttpContext.User.Identity.Name);
                    }
                    if (meeting == null)
                    {
                        filterContext.HttpContext.Response.Redirect("/workspace/home/index");
                    }
                    filterContext.Controller.ViewBag.Meeting = meeting;

                    filterContext.Controller.ViewBag.NewArticleCount = new Article().Count(
                        c => c.Conference_Id == meeting.Id && !c.IsDelete && c.Status == 0);

                    filterContext.Controller.ViewBag.WaitArticleCount = new Article().Count(
                        c => c.Conference_Id == meeting.Id && !c.IsDelete && new List<int>() { 2, 4, 6, 8, 10 }.Contains(c.Status));

                    break;
            }
        }

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            
        }
    }
}
